<?php
/**
+-----------------------------------------
 * Facile PHP System Framework
+-----------------------------------------
 * @description		日志管理类
 * @author	张辉
 * @version	 1.0
+-----------------------------------------
 */
namespace app\common\service;

use app\common\library\CommonFun;

class ElkLog {

    /**
     * 保存日志文件到syslog
     * @param $message
     */
    public static function save( $message ){
        if ( ENV == 'release' ){
            openlog('php-release', LOG_PID, LOG_LOCAL6);
        } else {
            openlog('php-test', LOG_PID, LOG_LOCAL6);
        }

        syslog(LOG_DEBUG, CommonFun::json_encode2( $message ) );
        closelog();
    }

    /**
     * @param $message
     * @param int $level
     * @param bool $backtrace
     */
    public static function write( $message, $level=400, $backtrace = false ) {
        if (! is_array ( $message )) {
            $message = array (
                'message' => $message
            );
        }

        $message['type'] = $level;
        defined('SN') && $message ['sn'] = SN;
        $backtrace && $message ['backtrace'] = debug_backtrace ();
        self::save( $message );
    }
}